<?php ob_start();
session_start();
require("twitter/twitteroauth.php");
require 'config/twconfig.php';
include("../config/config.php");


if (!empty($_GET['oauth_verifier']) && !empty($_SESSION['oauth_token']) && !empty($_SESSION['oauth_token_secret'])) {
    // We've got everything we need
    $twitteroauth = new TwitterOAuth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
// Let's request the access token
    $access_token = $twitteroauth->getAccessToken($_GET['oauth_verifier']);
// Save it in a session var
    $_SESSION['access_token'] = $access_token;
// Let's get the user's info
    $user_info = $twitteroauth->get('account/verify_credentials');
// Insert user's info
if(isset($user_info)&&(!isset($_SESSION['id']))){
	$chk=mysql_query("SELECT * FROM `my_user` WHERE `twitter_id`='".$user_info->id."' ")or die(mysql_error());	
	if(!mysql_num_rows($chk)){
		$sql=mysql_query("INSERT INTO `my_user` SET `twitter_id`='".$user_info->id."',
													`first_name`='".$user_info->name."',
													`bio`='".addslashes($user_info->description)."'
													")or die(mysql_error());
		$id=mysql_insert_id();
		if($sql){ 
			$sql2=mysql_query("SELECT * FROM `my_user` WHERE `id`=$id ")or die(mysql_error());	
			$rows=mysql_fetch_array($sql2);
			$_SESSION['id']=	$rows['id'];
			$_SESSION['fname']=	$rows['fname'];
			if(isset($_SESSION['business_url'])&&($_SESSION['business_url']!="")){
				header("location:../dashboard.php"); exit;
			}else{
				header("location:http://mycitydial.com"); exit;
			}
		}
	}else{
		$sql2=mysql_query("SELECT * FROM `my_user` WHERE `twitter_id`='".$user_info->id."' ")or die(mysql_error());	
		$rows=mysql_fetch_array($sql2);
		$_SESSION['id']=	$rows['id'];
		$_SESSION['fname']=	$rows['fname'];
		if(isset($_SESSION['business_url'])&&($_SESSION['business_url']!="")){
			header("location:../dashboard.php"); exit;
		}else{
			header("location:http://mycitydial.com"); exit;
		}
	}	
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
} else {
    // Something's missing, go back to square 1
    header('Location:../index.php');
}
?>
